import { Data, Render, Character, Player } from "#fansky";
import { Avatar } from "../../../miao-plugin/models/index.js";
import { DamageRes, Serv, ServReq, ProfileData, Profile } from "#fansky.models";

let ProfileDmg = {
    async teamDmg(e, roles, uid) {
        if (roles.length === 1) {
            const res = await this.getTeams(roles[0]);
            if (res && res.length) {
                roles = res;
            }
        } else if (!roles.length) {
            if (e.isSuper) return false
            e.reply('未指定队伍, 尝试获取面板前4名角色~', true, { recallMsg: 10 });
            // 更新面板
            let data = await Profile.teamDmg(e, uid);
            if (data.error) {
                return e.reply(data.error)
            }
            return await this.renderData(e, data)
        }

        let characters = []
        for (let role of roles) {
            let character = Character.get(role);
            if (!character) {
                return e.reply(`未能识别的角色：${role}`);
            }
            characters.push(character);
        }

        const avatars = {};
        const reqData = { uid, role_data: [] };
        const player = Player.create(uid);
        for (const role of characters) {
            let profile = player.getProfile(role.id);

            // 极限主角 -- 暂时这样吧
            if (e.isSuper && profile.char.isTraveler) {
                let _avatars = player._avatars
                if (!_avatars[role.id]) return false

                let avatar = _avatars[role.id]
                if (!avatar.isAvatar) {
                    let data = _avatars[role.id]
                    avatar = _avatars[role.id] = Avatar.create(_avatars[role.id])
                    avatar.setAvatar(data)
                }
                profile = avatar
            }

            if (!profile || !profile.hasData) {
                return e.reply(`UID: ${uid}缺少${role.name}, 请先【#更新面板】~`, false, { recallMsg: 10 });
            }
            // 角色数据
            let imgs = profile.char.getImgs(profile.costume)
            avatars[role.name] = await ProfileData.covRoleData(profile, imgs)
            // 转换为提瓦特角色数据
            const covProfileData = await ProfileData.covTeyvatData(profile)
            reqData.role_data.push(covProfileData);
        }
        // 队伍
        e.reply(`UID: ${uid}\n角色: ${characters.map(v => v.name).join(', ')}`);

        reqData.server = await Serv.getServ(uid, true);

        let { code, result, info } = await ServReq.teyvat(reqData)

        if (code !== 200 || !result) {
            logger.error(`>>>[错误信息] ${info}`);
            return e.reply(`提瓦特小助手接口无法访问或返回错误`, true);
        }
        let data = await DamageRes.teamDmgRes(result, avatars);

        return await this.renderData(e, data);
    },

    async renderData(e, data) {
        const renderData = {
            ...data,
            detail: e.detail,
            saveId: data.uid
        }
        return Render.render(e, 'dmg/damage', renderData)
    },

    async getTeams(alias) {
        if (typeof alias !== 'string') {
            return [];
        }
        const data = await Data.readYAML('config/alias.yaml');
        for (let [ name, value ] of Object.entries(data)) {
            if (alias === name || value.alias && value.alias.includes(alias)) {
                return alias === name && Array.isArray(value) ? value : value.chars;
            }
        }
        return [];
    }
}

export default ProfileDmg
